package com.huanxing.cloud.miniapp.config;

import com.huanxing.cloud.miniapp.api.entity.WxApp;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component

public class WechatConfigCache {

	// appId -> 配置（可从 appId 直接找配置）
	private final Map<String, WxApp> appIdMap = new ConcurrentHashMap<>();

	public void addConfigs(List<WxApp> configs) {
		configs.forEach(config -> appIdMap.put(config.getAppId(), config));
	}

	public WxApp getByAppId(String appId) {
		return appIdMap.get(appId);
	}

	public void updateConfig(WxApp config) {
		appIdMap.put(config.getAppId(), config);
	}

	public void removeByAppId(String appId) {
		appIdMap.remove(appId);
	}

	public Collection<WxApp> getAllConfigs() {
		return appIdMap.values();
	}

}
